package org.finesys.chat.core.base.document.source;

import org.finesys.chat.core.base.document.Metadata;
import org.finesys.common.core.util.ValidationUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileSystemSource implements DocumentSource {

    private final Path path;

    public FileSystemSource(Path path) {
        this.path = ValidationUtil.ensureNotNull(path, "path");
    }

    public static FileSystemSource from(Path filePath) {
        return new FileSystemSource(filePath);
    }

    public static FileSystemSource from(String filePath) {
        return new FileSystemSource(Paths.get(filePath));
    }

    public static FileSystemSource from(URI uri) {
        return new FileSystemSource(Paths.get(uri));
    }

    public static FileSystemSource from(File file) {
        return new FileSystemSource(file.toPath());
    }


    @Override
    public Metadata metadata() {
        return new Metadata()
                .put("absolute_directory_path", this.path.toAbsolutePath().toString())
                .put("fileName", this.path.getFileName().toString());
    }

    @Override
    public InputStream inputStream() throws IOException {
        return Files.newInputStream(this.path);
    }
}
